package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import to.Aluno;
import to.Curso;
import to.Matric_disciplina;
import to.Matricula;
import to.PeriodoLetivo;
import to.Turma;
import util.HibernateUtil;

public class ManutMatricDAO {

	public List<PeriodoLetivo> buscaTodosPeriodo() {

		List<PeriodoLetivo> listaRetorno = new ArrayList<PeriodoLetivo>();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(PeriodoLetivo.class,
					"per");
			criteria.addOrder(Order.desc("per.codLetivo"));
			listaRetorno = (ArrayList<PeriodoLetivo>) criteria.list();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return listaRetorno;
	}

	public PeriodoLetivo buscarPorCodigo(Integer codLetivo) {

		PeriodoLetivo p = new PeriodoLetivo();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(PeriodoLetivo.class,
					"pl");
			criteria.add(Restrictions.eq("pl.codLetivo", codLetivo));
			p = (PeriodoLetivo) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;
	}

	public Matricula buscaMatricPorCodigo(Integer codCurso, Integer idAluno,
			PeriodoLetivo p) {

		Matricula mt = new Matricula();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session
					.createCriteria(Matricula.class, "matric")
					.createAlias("matric.aluno", "al")
					.createAlias("matric.curso", "cur");
			criteria.add(Restrictions.eq("al.idOrion", idAluno));
			criteria.add(Restrictions.eq("matric.perLetivo", p));
			criteria.add(Restrictions.eq("cur.idOrion", codCurso));
			mt = (Matricula) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return mt;
	}

	public Aluno buscarAlunoPorCodigo(Integer idOrion) {

		Aluno a = new Aluno();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Aluno.class, "al");
			criteria.add(Restrictions.eq("al.idOrion", idOrion));
			a = (Aluno) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return a;
	}

	public List<Turma> buscaTurmasPorCurso(Curso cur) {

		List<Turma> listaTurma = new ArrayList<Turma>();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Turma.class, "t");
			criteria.add(Restrictions.eq("t.curso", cur));
			criteria.addOrder(Order.asc("t.serie"));
			listaTurma = (ArrayList<Turma>) criteria.list();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return listaTurma;
	}

	public Curso buscarCursoPorCodigoLegado(Integer idOrion) {

		Curso c = new Curso();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Curso.class, "cur");
			criteria.add(Restrictions.eq("cur.idOrion", idOrion));
			c = (Curso) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}

	public List<Matric_disciplina> buscaTurmasMatriculadas(Matricula mt) {

		List<Matric_disciplina> listaTurma = new ArrayList<Matric_disciplina>();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session.createCriteria(Matric_disciplina.class,
					"md").createAlias("md.turma", "t");
			criteria.add(Restrictions.eq("md.matricula", mt));
			criteria.addOrder(Order.asc("t.serie"));
			listaTurma = (ArrayList<Matric_disciplina>) criteria.list();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return listaTurma;
	}

	public Matric_disciplina buscaTurmaMatricula(Turma turma, Matricula mt) {

		Matric_disciplina t = new Matric_disciplina();
		Session session = null;

		try {
			session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session
					.createCriteria(Matric_disciplina.class, "m")
					.createAlias("m.turma", "tma")
					.createAlias("m.matricula", "matric");
			criteria.add(Restrictions.eq("tma.id", turma.getId()));
			criteria.add(Restrictions.eq("matric.id", mt.getId()));
			t = (Matric_disciplina) criteria.uniqueResult();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return t;
	}

}
